<template>
	<view class="draw-action bg-color-white flex flex-ai-center">
		<z-create-order v-model='showCreate' :product="product"></z-create-order>
		<!-- {{signUpStatus}} -->
		<!-- 报名未开始 -->
		<view style="width: 100%;" class="flex flex-js-sb flex-1 plr-16 flex-ai-center" v-if="signUpStatus==1">
			<view class=" font-size-14">
				还剩
				<text class="font-color-theme">{{inventory}}</text>
				份
			</view>
			<w-button :disabled="true" style='width:350rpx;'>活动未开始</w-button>
		</view>  
		<!-- 报名已开始-->
		<view class="pl-16" v-else-if="signUpStatus==2">
			  
			<w-button v-if="codeInfo.my_draw_num" style='width: 686rpx' @click='signUp'>进行中，获取签号（{{codeInfo.my_draw_num}}）</w-button>
			<w-button v-else style='width: 686rpx'  @click="changezlShow">暂无抽签次数，快去分享</w-button>
		</view>
		
		
		<!-- 报名已结束、已参与、无抽签结果-->
		<view class="plr-16" v-if='signUpStatus==3&&((!isDrawed)||codeInfo.whether_res==-1)&&codeInfo.my_code.length'>
			<w-button  style='width: 686rpx' :disabled="true" >等待抽签</w-button>
		</view>
		
		<!-- 报名已结束、未参与-->
		<view class="plr-16" v-if='signUpStatus==3&&codeInfo.my_code.length==0'>
			<w-button  style='width: 686rpx' :disabled="true">您未参与本次活动</w-button>
		</view>
		
		
		<!-- 报名已结束、已参与、已有抽签结果-->
		<view class="plr-16" v-if='signUpStatus==3&&codeInfo.whether_res>-1&&codeInfo.my_code.length'>
			<!-- 未中签 -->
			<template v-if="codeInfo.whether_res == 0">
				<w-button  style='width: 686rpx' :disabled="true">很遗憾，您未中签</w-button>
			</template>
			
			<!-- 已中签 -->
			<template v-else>  
					<!-- 未开售 -->
					<w-button style='width: 686rpx' :disabled="true" v-if="saleStatus==1">
							<u-count-down :time="distanceStartTime" format="HH:mm:ss后开售"></u-count-down>
					</w-button>
					
					<!-- 已开售 -->
					<w-button style='width: 686rpx' @click="showCreate=true" v-if="saleStatus==2">
							已中签，立即购买
					</w-button>
					
					<!-- 已售罄 -->
					<w-button style='width: 686rpx'  @click='toMarket' v-if="saleStatus==3">
							进入市场
					</w-button>
					
					
					<!-- 发售时间结束 -->
					<w-button style='width: 686rpx' @click='toMarket' v-if="saleStatus==4">
							进入市场
					</w-button>
					
			</template>
		</view>

		<u-popup :show='showSignReslut' round="12" mode='center' bgColor='#fff'>
			<view class="pt-12" style='width:600rpx'>
				<view class="font-color-dark text-center font-w-bold">
					温馨提示
				</view>
				<view class="content text-center font-size-14 font-color-dark_light_1 pt-12 pb-24">
					已抽签，签号：{{signUpInfo.code}}
				</view>
				<view class="flex" style="height: 88rpx;border-top: 1px solid #cacaca;">
					<view class="flex-1 font-color-white_light_1 flex-center" style="border-right : 1px solid #cacaca;"
						@click="showSignReslut=false">
						取消
					</view>
					<view class="flex-1 font-color-theme flex-center" @click="showSignReslut=false">
						确认
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import drawMixin from '@/mixins/draw.js'
	import productMixin from '@/mixins/product.js'
	import {
		signUpDraw,
		
		// queryDrawStatus
	} from '@/api/order/order.js'
	import {drawMycode} from '@/api/draw/index.js'
	import dayjs from 'dayjs'
	export default {
		mixins: [drawMixin, productMixin],
		name: "z-draw-action",
		props: {
			product: {
				type: Object,
				default () {
					return {
						batch: {}
					}
				}
			},
			codeInfo:{
				type: Object,
				default () {
					return {
						batch: {}
					}
				}
			},
			invitFriend:{
				type:[String,Number]
			}
		},
		watch: {
			product:{
				deep:true,
				immediate:true,
				handler(info){
					if(info.id) {
						// this.getDrawMycode()
					}
				}
			},			
			signUpStatus: {
				deep: true,
				immediate: true,
				handler(v) {
					// let isSignUpDone=new Date()-dayjs(this.drawBase.draw_time)>0
					if (this.drawBase.id && !this.isLoad) {
						// this.getDrawStatus()
					}
				}
			}
		},
		data() {
			return {
				showSignReslut: false,
				showCreate: false,
				isLoad: false,
				statusInfo: {},
				signUpInfo: {}
			};
		},
		methods: {
			//获取我的抽签码/结果
			// getDrawMycode(){
			// 	let data = {
			// 		goods_id:this.drawBase.id,
			// 		goods_type:this.product.goods_type
			// 	}
			// 	drawMycode(data).then( res => {
			// 		this.codeInfo = res.data
			// 		console.log(this.codeInfo)
			// 	}).catch(err => {})
			// },
			// getDrawStatus() {
			// 	this.isLoad = true;
			// 	queryDrawStatus({
			// 		goods_type: this.product.goods_type,
			// 		goods_id: this.drawBase.id
			// 	}).then(res => {
			// 		this.statusInfo = res.data;
			// 		this.$emit('drawStatusChange', res.data)
			// 	})
			// },
			toMarket() {
				uni.switchTab({
					url: '/pages/market/market'
				})
			},
			//抽签报名
			signUp() {
				uni.showLoading({
					title: '报名中'
				})
				let data = {
					goods_id: this.drawBase.id,
					goods_type: this.product.goods_type,
				}
				if(this.invitFriend) {
					console.log(uni.getStorageSync('invite_code'),'yaoqingdezhi');
					data.helpucode = uni.getStorageSync('invite_code')
					data.newuser = uni.getStorageSync('isNewUser')?1:0
				}  
				signUpDraw(data).then(res => {
					this.signUpInfo = res.data;
					this.showSignReslut = true;
					uni.removeStorageSync('invitFriend')
					uni.$emit('signUpChange')
				})
			},
			changezlShow(){
				uni.$emit('changezlShow')
			}
		}
	}
</script>

<style lang="scss">
	.draw-action {
		/deep/ .u-count-down__text {
			color: #979997 !important;
			font-size: 32rpx !important;
		}
	}

	.draw-action {
		width: 750rpx;
		height: 120rpx;
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 1;

	}
</style>
